import Vue from 'vue'
import VueRouter from 'vue-router'

// 解决Element-UI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err);
}

//运行路由插件
Vue.use(VueRouter)

// 路由懒加载
const Login = () => import('components/content/Login.vue')
const Register = () => import('components/content/Register.vue')
const Home = () => import('views/Home.vue')
const Category = () => import('views/Category.vue')
const GoodsInfo = () => import('views/GoodsInfo.vue')
const CategorySearch = () => import('views/CategorySearch.vue')
const Message = () => import('views/Message.vue')
const Profile = () => import('views/Profile.vue')
const ProfileInfo = () => import('views/ProfileInfo.vue')
const Logistics = () => import('views/Logistics.vue')
const AddressManage = () => import('views/AddressManage.vue')
const AddressUpdate = () => import('views/AddressUpdate.vue')
const News = () => import('views/News.vue')
const NewsDetail = () => import('views/NewsDetail.vue')
const GoodsDetail = () => import('views/GoodsDetail.vue')
const ShopCart = () => import('views/ShopCart.vue')
const AddShipAddress = () => import('views/AddShipAddress.vue')
const Template = () => import('views/Template.vue')

let routes = [ //路由配置
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '/login',
        component: Login,
        name: 'login',
        meta: {
            title: '登录'
        }
    },
    {
        path: '/register',
        component: Register,
        name: 'register',
        meta: {
            title: '注册'
        }
    },
    {
        path: '/home',
        component: Home,
        name: 'home',
        meta: {
            title: '首页'
        }
    },
    {
        path: '/news',
        component: News,
        name: 'news',
        meta: {
            title: '新闻资讯'
        }
    },
    {
        path: '/news/:id',
        component: NewsDetail,
        name: 'NewsDetail',
        props: true,
        meta: {
            title: '新闻详情'
        }
    },
    {
        path: '/photo',
        component: Template,
        name: 'photo',
        meta: {
            title: '图片分享'
        }
    },
    {
        path: '/message',
        component: Message,
        name: 'message',
        meta: {
            title: '留言反馈'
        }
    },
    {
        path: '/video',
        component: Template,
        name: 'video',
        meta: {
            title: '视频专区'
        }
    },
    {
        path: '/profile',
        component: Profile,
        name: 'profile',
        meta: {
            title: '我的'
        }
    },
    {
        path: "/profile/info",
        component: ProfileInfo,
        name: "profileInfo",
        meta: {
            title: "我的信息"
        }
    },
    {
        path: '/logistics',
        component: Logistics,
        name: 'logistics',
        meta: {
            title: '物流查询'
        }
    },
    {
        path: '/shopCart',
        component: ShopCart,
        name: 'shopCart',
        meta: {
            title: '购物车'
        }
    },
    {
        path: '/digital',
        component: Template,
        name: 'digital',
        meta: {
            title: '数码电器'
        }
    },
    {
        path: '/category',
        component: Category,
        name: 'category',
        meta: {
            title: '分类'
        }
    },
    {
        path: '/category/goodsInfo',
        component: GoodsInfo,
        name: 'goodsInfo',
        meta: {
            title: '商品详情'
        }
    }, {
        path: "/category/search",
        component: CategorySearch,
        name: "categorySearch",
        props: true,
        meta: {
            title: '商品搜索'
        }
    }, {
        path: "/category/detail",
        component: GoodsDetail,
        name: "goodsDetail",
        props: true,
        meta: {
            title: '商品详情'
        }
    }, {
        path: "/addressManage",
        component: AddressManage,
        name: "addressManage",
        props: true,
        meta: {
            title: '地址管理'
        }
    }, {
        path: "/addShipAddress",
        component: AddShipAddress,
        name: "addShipAddress",
        props: true,
        meta: {
            title: '添加地址'
        }
    }, {
        path: "/addressUpdate",
        component: AddressUpdate,
        name: "addressUpdate",
        props: true,
        meta: {
            title: '编辑收货地址'
        }
    }
];

//创建路由实例对象router
let router = new VueRouter({
    mode: 'history', //使用history模式
    routes,
    linkActiveClass: 'active',
    linkExactActiveClass: 'exact-active'
})

//导航钩子函数
router.beforeEach((to, form, next) => {
    //路由发生改变修改页面title
    if (to.meta.title) {
        document.title = to.meta.title
    }
    next()
})
//暴露路由对象属性
export default router